/**
 * @class 多语言文本（扩展的）
 * @author DuskyHuang 圣子
*/

import { _decorator, Component, Label } from 'cc';
import { DeepNameType, StarNameType } from 'const';
import { ui } from 'proxies';
import { CelestialNameMap } from '../celestial';
import { getI18nConfig } from 'helpers';
const { ccclass, disallowMultiple, requireComponent, executeInEditMode } = _decorator;

export enum CelestialNameType {
    Pure = 1,
    ConfigKey = 2,
    Config = 3,
    Star = 4,
    Deep = 5,
}

@ccclass('cocosmos.label-18n-adv')
@disallowMultiple
@executeInEditMode(true)
@requireComponent(Label)
export class LabelI18nAdvanced extends Component {


    public get type() { return this._type; }
    public set type(arg: CelestialNameType) { this._type = arg; }

    public get string(): string { return this._label?.string; }

    public set pure(arg: string) { this._type = CelestialNameType.Pure; if (this._label) this._label.string = arg; }

    public set key(en: string) {
        this._type = CelestialNameType.ConfigKey;
        this._key = en;
        this._config = getI18nConfig(en);
        if (!this._config) {
            console.warn('i18n-adv lost config for:', en);
            if (this._label) this._label.string = en;
            return;
        }
        this._lang = null;
    }

    public set config(config: i18nConfig) {
        this._type = CelestialNameType.Config;
        this._config = config;
        this._lang = null;
    }

    public setMapType(map: CelestialNameMap, type: CelestialNameType.Star | CelestialNameType.Deep) {
        this._type = type;
        this._map = map ?? {};
        this._lang = null;
    }

    protected onLoad(): void {
        this._label = this.getComponent(Label);
    }

    protected lateUpdate(): void {
        if (this._type === CelestialNameType.Pure) return;
        if (!this._label) return;
        if (ui.language != this._lang) {
            this._lang = ui.language;
            this._updateString(); return;
        }
        if (this._type === CelestialNameType.Star) {
            if (this._enum != ui.starNameType) {
                this._enum = ui.starNameType;
                this._updateString();
            }
            return;
        }
        if (this._type === CelestialNameType.Deep) {
            if (this._enum != ui.deepNameType) {
                this._enum = ui.deepNameType;
                this._updateString();
            }
            return;
        }
    }

    private _updateString(): void {
        if (this._type === CelestialNameType.ConfigKey || this._type === CelestialNameType.Config) {
            if (this._config) this._label.string = this._config[this._lang];
        } else if (this._type === CelestialNameType.Star || this._type === CelestialNameType.Deep) {
            const item = this._map[this._enum];
            if (item) {
                this._label.string = isStr(item) ? item : item[this._lang];
            }
        }
    }

    private _label: Label;
    private _type: CelestialNameType = CelestialNameType.Pure;
    private _key: string = null;
    private _lang: language = null;
    private _config: i18nConfig;
    private _enum: StarNameType | DeepNameType;
    private _map: CelestialNameMap = {};
}